package com.example.img;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {
	private Button button;
	private ImageView imageView;
	private final String IMAGE_PATH = "http://img.kejixun.com/2013/0731/20130731101830109.jpg";
	Drawable background;
	Handler handler = new Handler() {

		public void handleMessage(Message msg) {
			if (msg.what == 0) {  
				
				imageView.setBackground(background);

			}

		};

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.v("xianchen", Thread.currentThread().getName().toString())	; 
		button = (Button) this.findViewById(R.id.button1);
		imageView = (ImageView) this.findViewById(R.id.imageView1);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			Thread t1 =  new Thread(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
					Log.v("xianchen", Thread.currentThread().getName().toString())	; 
						HttpClient client = new DefaultHttpClient();
						HttpGet get = new HttpGet(IMAGE_PATH);
						try {
							HttpResponse httpResponse = client.execute(get);
							HttpEntity entity = httpResponse.getEntity();
							InputStream inputStream = entity.getContent();
							Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
							BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
							background = bitmapDrawable;
							BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
							StringBuilder stringBuilder  = new StringBuilder();
							String crr= null;
							while((crr = bufferedReader.readLine())!=null){
								stringBuilder.append(crr+"\n");
								
							} 
							Log.v("input", stringBuilder.toString());
							Message msg = Message.obtain();
							msg.what = 0; 
							handler.sendMessage(msg);
						} catch (ClientProtocolException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				});
				t1.start();
				 
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
